/**
  * 用户相关接口
  * @author: Ruan Jiazhen
  * @date: 2024-04-29 09:36:04
* */
// XXX 部分接口已废弃，可考虑删除。以及部分接口可以迁移到其他接口文件中，进行重新分类
import request, { reqPost } from './request';

/**
 * 获取微信登录code
 * @returns
 */
export const requestCode = () => new Promise((resolve, reject) => {
  uni.login({
    provider: 'weixin',
    onlyAuthorize: true,
    success: (res) => {
      console.log('requestCode', res);
      resolve(res);
    },
    fail: (err) => {
      reject(err);
    },
  });
});

/**
 * 使用微信登录code进行登录，获取token，并且登记邀请人
 * @param code 微信登录code
 * @param inviteKey 邀请码
 */
export const requestToken = (code: string, inviteKey: string) => request.getToken('/activity/wxPhoneLogin', {
  data: {
    code,
    invite_key: inviteKey,
  },
});

/**
 * 获取2023年618活动相关的用户信息
 */
export const requestInfo618 = () => request.get('/activityMay2023/getMayLoginInfo');

export const requestInfo = () => request.post('/sjsp/getUserInfo');

/**
 * 修改头像昵称
 * @param avatarPath 头像地址
 * @param name 昵称
 */
export const reqUpdateAvatarAndName = (avatarPath: string, name: string) => reqPost('/host/personalInformation', {
  avator: avatarPath,
  emcee_name: name,
});

/**
 * 领取登录喵豆
 */
export const requestLoginMiaodou = () => request.post('/activityMay2023/gainMiaoDou1');

/**
 * 领取任务喵豆
 */
export const requestTaskMiaodou = () => request.post('/activityMay2023/gainMiaoDou2');

/** 获取今日喵豆和本周喵豆 */
export const requestTodayAndWeekMiaodou = () => request.get('/activityMay2023/miaoDouInfo');

/** 获取喵豆明细列表 */
export const requestMiaodouList = (page: number, pageSize: number) => request.get('/activityMay2023/miaoDouList');

/** 获取抽奖记录列表 */
export const requestLotteryList = (page: number, pageSize: number) => request.get('/activityMay2023/lotteryList');

/** 获取我的邀请列表 */
export const requestMyInviteList = (page?: number, showCount?: number) =>
  // 判断是否传入了参数，传入了哪些参数就把哪些参数传给后端
  // const params: any = {}
  // if (page) params.currentPage = page
  // if (showCount) params.showCount = showCount
  request.get('/activityMay2023/inviteList');

/** 进行实名认证 */
export const requestRealName = (name: string, idCard: string) => request.post('/activityMay2023/certification', {
  data: {
    name,
    idcard: idCard,
  },
});

/** 发出绑定支付宝请求 */
export const requestBindAlipay = (name: string, alipay: string) => request.post('/host/updateZhiFuBaoInfo', {
  data: {
    realName: name,
    zhiFuBaoPhone: alipay,
  },
});

/** 获取手机验证码 */
export const getVerifyCode = (phone: string) => {
  const source = '1'; // 1:注册  2：修改密码
  return request.post(
    '/dengLuHm/faSongCode',
    {
      data: {
        phonenumber: phone,
        source,
      },
    },
  );
};

/** 注册
 * @param inviteKey 邀请码
 * @param phone 手机号
 * @param code 验证码
 * @param password 密码
 */
export const requestRegister = (phone: string, code: string, password: string, codeId: string, regToken: string, inviteKey = '00000') => {
  console.log('register', phone, code, password, inviteKey);
  return request.post('/dengLuHm/zhuCeNe', {
    data: {
      phonenumber: phone,
      code,
      passwd: password,
      id: codeId,
      token: regToken,
      invite_key: inviteKey,
    },
  });
};

/**
 * 使用账号密码登录
 * @param phone 手机号
 * @param password 密码
 */
export const requestLoginByPassword = (phone: string, password: string) => request.post('/dengLuHm/gogogo', {
  data: {
    phonenumber: phone,
    passwd: password,
  },
});

/**
 * 让后端记录邀请人
 * @param inviteUserId 邀请人id
 */
export const requestRecordInvite = (inviteUserId: string) => request.get('/activityMay2023/saveInviter', { data: { inviter_id: inviteUserId } });

/**
 * 只使用邀请码让后端记录邀请人
 * @param inviteKey 邀请码
 */
export const requestRecordInviteByInviteKey = (inviteKey: string) => request.get('/activityMay2023/saveInviter', { data: { invite_key: inviteKey } });

/**
 * 通知后端已订阅
 */
export const requestSubscribe = () => request.get('/activityMay2023/addDySize');

/**
 * 获取分享二维码
 * @param page 页面路径
 * @param scene 场景值，即邀请人的邀请key
 * @param appid 小程序appid
 */
export const requestShareQrCode = (page: string, scene: string, appid: string) => request.get('/newHuoDongHm/getSmallCodeInviter2', {
  data: {
    page,
    scene,
    appid,
  },
});

/**
 * 绑定小程序openID
 * @params code 进行登录授权时返回的code
 */
export const requestBindOpenId = (code: string) => request.getToken('/activity/bindActivityOpenId', { data: { code } });
